<?php
namespace app\admin\controller;
use app\admin\model\AuthGroup;
use app\admin\model\Admin as M;
use app\admin\model\AuthGroupAccess;
use think\facade\Request;
use think\facade\View;

class Admin extends Base
{

    // 管理员列表
    public function index()
    {
        if (Request::isAjax()) {
            $adminModel = new \app\admin\model\Admin();
            return $adminModel->tableData(input('param.'));
        }else{
            return View::fetch('index');
        }
    }
    // 管理员添加
    public function add(){
        if(Request::isPost()){
            $data = Request::post();
            if (empty($data['group_id'])) {
                $this->error('请选择用户组');
            } else {
                $group_id = $data['group_id'];
                unset($data['group_id']);
            }
            //单独验证密码
            if (empty($data['password'])) {
                $this->error('密码不能为空');
            }
            //验证
            $result = $this->validate($data, 'Admin');
            if (true !== $result) {
                // 验证失败 输出错误信息
                $this->error($result);
            }

            $data['password']  = md5(trim($data['password']));
            $data['logintime'] = time();
            $data['loginip']   = Request::ip();
            //添加
            $result = \app\admin\model\Admin::create($data);
            if ($result) {
                AuthGroupAccess::create([
                    'uid'      =>  $result->id,
                    'group_id' =>  $group_id
                ]);
                $result['status'] = true;
                $result['msg'] = '添加成功';
                return $result;
            } else {
                $result['status'] = false;
                $result['msg'] = '失败';
                return $result;
            }
        } else {
            $auth_group = AuthGroup::where('status','=',1)
                ->select();
            $view = [
                'authGroup' => $auth_group,
                'info'      => null
            ];
            View::assign($view);
            $return['status'] = true;
            $return['msg'] = '成功';
            $return['data'] = View::fetch();
            return $return;

        }
    }

    // 管理员删除
    public function del(){
        $id = Request::post('id');
        if ($id >1) {
            \app\admin\model\Admin::destroy($id);
            return json(['status'=>true, 'msg'=>'删除成功!']);
        } else {
            return json(['status'=>false, 'msg'=>'超级管理员不可删除!']);
        }
    }

    // 管理员批量删除
    public function adminSelectDel(){
        $id = Request::post('id');
        if ($id) {
            $ids = explode(',', $id);
        }
        if (in_array('1',$ids)) {
            return json(['error'=>1, 'msg'=>'超级管理员不可删除!']);
        }
        Admin::destroy($id);
        return json(['error'=>0, 'msg'=>'删除成功!']);
    }

    // 管理员状态修改
    public function state(){
        if (Request::isPost()) {
            $id = Request::post('id');
            if (empty($id)) {
                return json(['error'=>1, 'msg'=>'用户ID不存在!']);
            }
            if ($id==1) {
                return json(['error'=>1, 'msg'=>'超级管理员不可修改状态!']);
            }
            $admin = \app\admin\model\Admin::find($id);
            $status = $admin['status'] == 1 ? 0 : 1;
            $admin->status = $status;
            $admin->save();
            return json(['error'=>0, 'msg'=>'修改成功!']);
        }
    }

    // 管理员修改
    public function edit(){
        if (Request::isPost()) {
            $data = Request::post();
            $password = $data['password'];
            $map[] = ['id','<>',$data['id']];
            $where['id'] = $data['id'];

            $group_id = $data['group_id'];
            unset($data['group_id']);
            $data['status']=isset($data['status'])?1:0;
            $result = $this->validate($data,'Admin');
            if (true !== $result) {
                $this->error($result);
            }

            if ($password) {
                $data['password'] = input('post.password','','md5');
            }else{
                unset($data['password']);
            }

            \app\admin\model\Admin::update($data, $where);
            AuthGroupAccess::update([
                'group_id' =>  $group_id
            ], ['uid'=>$data['id']]);
            $res['status'] = true;
            $res['msg'] = '添加成功';
            return $res;

        } else {
            if (Request::param('id')) {
                $auth_group = AuthGroup::select();
                $admin = \app\admin\model\Admin::find(Request::param('id'));

                $admin['group_id'] = AuthGroupAccess::where('uid', $admin['id'])
                    ->value('group_id');


                $view = [
                    'info'      => $admin,
                    'authGroup' => $auth_group,
                ];

                View::assign($view);
                //return View::fetch('admin_add');


                $result['status'] = true;
                $result['msg']    = '成功';
                $result['data']   = View::fetch('add');

                return $result;
            }
        }
    }
}